package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.dongdongshop.pojo.TbContent;
import com.dongdongshop.pojo.TbContentCategory;
import com.dongdongshop.result.Result;
import com.dongdongshop.result.ResultEnum;
import com.dongdongshop.service.ContentCategoryService;
import com.dongdongshop.service.ContentService;
import com.dongdongshop.util.PageUtil;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("content")
public class ContentController {

    private static final Logger logger = LoggerFactory.getLogger(ContentController.class);

    @Reference
    private ContentService contentService;

    @Reference
    private ContentCategoryService contentCategoryService;

    @RequestMapping("toContentShow")
    public String toContentShow(Model model){
        List<TbContentCategory> tbContentCategories = contentCategoryService.getContentCategory();
        model.addAttribute("categoryList",tbContentCategories);
        return "admin/content";
    }

    @RequestMapping("getListContent")
    @ResponseBody
    public Result<PageUtil<TbContent>> getListContent(@RequestParam(value = "pageNumber", defaultValue = "1") Integer pageNumber,
                                                      @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        try {
            PageInfo<TbContent> pageInfo = contentService.getListContent(pageNumber,pageSize);
            PageUtil<TbContent> pageUtil = new PageUtil<>();
            pageUtil.setPageNumber(pageNumber);
            pageUtil.setPageSize(pageSize);
            pageUtil.setList(pageInfo.getList());
            pageUtil.setTotal(pageInfo.getTotal());
            return Result.jsonResponse(ResultEnum.OK).setData(pageUtil);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("insertContent")
    @ResponseBody
    public Result insertContent(TbContent content){
        try {
            int i = contentService.insertContent(content);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("toUpdateContent")
    @ResponseBody
    public Result toUpdateContent(@RequestParam("id") Long id){
        try {
            TbContent content = contentService.selectInfoById(id);
            return Result.jsonResponse(ResultEnum.OK).setData(content);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    //修改数据
    @RequestMapping("updateContentInfo")
    @ResponseBody
    public Result updateContentInfo(TbContent content){
        try {
            int i = contentService.updateContentInfo(content);
            return Result.jsonResponse(ResultEnum.OK);
        }catch (Exception e){
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("deleteBatch")
    @ResponseBody
    public Result deleteBatch(@RequestParam("ids[]") Long[] ids){
        try {
            int i = contentService.deleteBatch(ids);
            return Result.jsonResponse(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

    @RequestMapping("updateStatusById")
    @ResponseBody
    public Result updateStatusById(@RequestParam("ids[]") Long[] ids,@RequestParam("status") String status){
        try {
            int i = contentService.updateStatusById(ids,status);
            return Result.jsonResponse(ResultEnum.OK);
        } catch (Exception e) {
            logger.error("系统报错",e);
            return Result.jsonResponse(ResultEnum.INTERNAL_SERVER_ERROR);
        }
    }

}
